﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kalficom
{
    public partial class frmUser : Form
    {
        public frmUser()
        {
            InitializeComponent();
            conf.Icon(this);
        }

        Libraies.db db = new Libraies.db();
        Query.qry_User query = new Query.qry_User();
        
        private void frmUser_Load(object sender, EventArgs e)
        {  
            // Create YesNo bind into datagridcombobox
            tblYesNo.Rows.Add(new object[] {"Yes", 1});
            tblYesNo.Rows.Add(new object[] {"No", 0});

            // Read user record from database
            db.FillDataSet(query.get, tblUser);

            this.WindowState = FormWindowState.Maximized;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if ((new Forms.frmUser_CreateModify()).ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Read user record from database
                db.FillDataSet(query.get, tblUser, false);
            }
        }

        private void dgUser_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message.Replace("user_login", "Login"), (new Libraies.msg()).captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void btnMainMenu_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dgUser_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            Forms.frmUser_CreateModify frm = new Forms.frmUser_CreateModify();
            frm.Tag = dgUser.Rows[e.RowIndex].Cells["user_id"].Value;

            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Read user record from database
                db.FillDataSet(query.get, tblUser, false);
            }
        }

        private void dgUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == dgUser.Columns["user_del"].Index) 
            {
                Libraies.msg msg = new Libraies.msg();
                if (MessageBox.Show(msg.deleteMsg, msg.captionMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    db.ExecuteNonQuery(string.Format(query.deleted, dgUser.CurrentRow.Cells["user_id"].Value));
                    tblUser.Select(string.Format("user_id={0}", dgUser.CurrentRow.Cells["user_id"].Value))[0].Delete();
                }
            }
        }
    }
}
